package com.ajing.logextractor.extractor;

import com.ajing.logextractor.config.ExtractConfig;
import lombok.extern.slf4j.Slf4j;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Slf4j
public class RegexExtractor extends AbstractExtractor {

    private final Pattern pattern;

    public RegexExtractor(String pattern, ExtractConfig subExtractConfig) {
        super(pattern, subExtractConfig);
        this.pattern = Pattern.compile(pattern);
    }

    @Override
    public String extract(String input) {
        Matcher matcher = pattern.matcher(input);
        if (matcher.find()) {
            try {
                return matcher.group(1);
            } catch (Exception e) {
                log.error(e.getMessage(), e);
            }
        }
        return input;
    }
}
